#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;
const int M = 1e6 + 5;
int n, m;
int len[N], deg[N], ans[N];
int head[N];
int ver[M], Next[M], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) {
    int id, v;
    cin >> id >> len[i];
    cin >> v;
    while (v) {
      add(v, i);
      deg[i]++;
      cin >> v;
    }
  }
  queue<int> q;
  rep(i, 1, n) if (!deg[i]) {
    q.push(i);
    ans[i] = len[i];
  }
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      ans[v] = max(ans[v], ans[u] + len[v]);
      if (--deg[v] == 0) q.push(v);
    }
  }
  int mx = 0;
  rep(i, 1, n) mx = max(mx, ans[i]);
  cout << mx;
  return 0;
}